package org.goodev.arch;

import android.arch.lifecycle.LifecycleActivity;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class NormalActivity extends LifecycleActivity {

    private TextView mTextView;
    private LocationMgr mLocationMgr;
    private LocationListener mGpsListener = new MyLocationListener();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTextView = (TextView) findViewById(R.id.text);
        mLocationMgr = new LocationMgr(getLifecycle(), mGpsListener, getApplicationContext());
    }

    @Override
    protected void onResume() {
        super.onResume();
        Util.checkUserStatus((Boolean result) -> {
            // 这个检测用户状态的回调函数如果在 onPause 调用后才返回会怎么样？
            if (result) {
                mLocationMgr.enable();
            }
        });
    }

    private class MyLocationListener implements LocationListener {
        @Override
        public void onLocationChanged(Location location) {
            mTextView.setText(location.getLatitude() + ", " + location.getLongitude());
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            Toast.makeText(NormalActivity.this,
                    "Provider status changed: " + provider, Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onProviderEnabled(String provider) {
            Toast.makeText(NormalActivity.this,
                    "Provider enabled: " + provider, Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onProviderDisabled(String provider) {
            Toast.makeText(NormalActivity.this,
                    "Provider disabled: " + provider, Toast.LENGTH_SHORT).show();
        }
    }
}
